var PostData = require('PostData.js');
var plublic = require('plublic.js');
var app = getApp();
var sessionId = wx.getStorageSync('customerUuid');
var token = wx.getStorageSync('token');
var customerUuid = wx.getStorageSync('customerUuid');
module.exports = {
  transDate: transDate,
  getnavigate: getnavigate,
  getscore: getscore,
  gettime: gettime,
  addFavorite: addFavorite,
  updateinfo: updateinfo,
  ActionTyleComp: ActionTyleComp,
  upload_file: upload_file,
  wxupload_file: wxupload_file,
  txupload_file: txupload_file,
  shupload_file: shupload_file,
  pjupload_file: pjupload_file,
  video: video,
  modifyCustomerAddress: modifyCustomerAddress,
  Getinfo: Getinfo,
  confirmReceive: confirmReceive,
  deleteorder: deleteorder,
  CheckMobile: CheckMobile,
  Checkpsw: Checkpsw,
  Getreplace: Getreplace,
  Getorderopvalues: Getorderopvalues,
  getproductAndAttrIds: getproductAndAttrIds,
  getproductUuidAndSkuNoIdArr: getproductUuidAndSkuNoIdArr,
  getStorePomotionDefineProducts: getStorePomotionDefineProducts,//促销
  removeByValue: removeByValue,
  freshtime: freshtime,
  getFormId: getFormId, //获取FormId
  djsfreshtime: djsfreshtime,
  richFormat: richFormat,//富文本,
  adLink: adLink,//广告链接
  luYin: luYin,
  getCartNum: getCartNum,
  iconPath: plublic.ICON_PATH,
}

function getCartNum(that){
  var info = {
    'customerUuid': wx.getStorageSync('customerUuid'),
    'token': wx.getStorageSync('token'),
    'sessionId': wx.getStorageSync('sessionId')
  }
  var op = {
    'data':info
  }
  PostData(op,function(res){
    if(res.data.return_code=='0'){
      wx.setStorageSync('cartsNum', res.data.cartsNum);
      var cartsNum = res.data.cartsNum.toString();
      if (cartsNum>0){
        if (that) {
          that.setData({
            cartsNum: cartsNum
          })
        } else {
          wx.setTabBarBadge({
            index: 2,
            text: cartsNum,
          })
        }
      } else {
        wx.removeTabBarBadge({
          index: 2,
        })
      }
      
     
    }

  },'getCartNum',2)
}
// 广告图链接
function adLink(e){
  // P_L_O_0 进入商品列表页(品牌) 绑定设备
  // P_0  进入商品详情页
  // A_0  进入活动内容页
  // A_0
  // P_L_C_0  进入商品列表页(分类)
  var actioncode = e.currentTarget.dataset.actioncode;
  var actionid = e.currentTarget.dataset.actionid;
  if (actioncode === 'A_0' && actionid=='0') {
    // 促销集合页
    wx.navigateTo({
      url: '/pages/other/preferential',
    })
    return

  }
  else if(actioncode == 'V_L_0'){
    //商品列表
    console.log(e)
    wx.navigateTo({
     
      url: '/pages/product/commodityList?actionUuid=' + e.currentTarget.dataset.actionida,
    })
  }
  else if (actioncode == 'W_M_G'){
    console.log(actionid)

    // wx.redirectTo({
    //   url: "'" + actionid+"'",
    // })
    wx.switchTab({
      url: "'" + actionid + "'"
    })
   
  }
   else if (actioncode === 'A_0' && actionid.indexOf("https") == -1) {
    console.log(actionid)
    // 促销列表
    // 单品 1  店铺2 限量购5 买赠 6
    if (actionid=='3'){
      // 秒杀
      wx.navigateTo({
        url: '/pages/activity/seckill',
      })
    } else if (actionid=='4'){
    // 拼团
      wx.navigateTo({
        url: '/pages/activity/spell',
      })
    } else if (actionid == '7') {
      // 拼团
      wx.navigateTo({
        url: '/pages/other/couponExchange',
      })
    } else if (actionid == '8'){
      //跳转到整车优惠券
      console.log('2222222222')
      wx.navigateTo({
        url: '/pages/mall/coupon?scope=' + '2',
      })
    } else if (actionid == '100'){
      //跳转到活动介绍
      wx.navigateTo({
        url: "/pages/activity/introduce"
      })
    }
    else{
      // 其他类型
      wx.navigateTo({
        url: '/pages/mall/promotion?active=' + actionid,
      })
    }
    
  } else if (actioncode === 'A_0' && actionid.indexOf("https") != -1) {
    // 促销外链
    wx.setStorageSync('actionid', actionid)
    wx.removeStorageSync('returnShow')
    wx.switchTab({
      url: '/pages/home/promotion',
    })
  } else if (actioncode === 'P_0') {
    // 商品详情页
    wx.navigateTo({
      url: '/pages/mall/details?uuid=' + actionid,
    })
  } else if (actioncode === 'P_L_O_0') {
    wx.navigateTo({
      url: '/pages/my/binding/index',
    })
  }
}
// 促销
function getStorePomotionDefineProducts(that, promotionType, state=0) {
  // promotionType 促销类型 1 单品 2店铺 3 秒杀 4团购 5 预售
  // state  0即将开始  1 已开始
  wx.showLoading({
    title: '加载中',
  })
  var info = {
    'customerUuid': wx.getStorageSync('customerUuid'),
    'promotionType': promotionType,
    'state': state
  }
  var op = {
    'data': info
  }
  PostData(op, function (res) {
    wx.hideLoading()
    if (res.data.return_code == '0') {
      that.setData({
        promotionProducts: res.data.promotionProducts
      })
    }

  }, 'getStorePomotionDefineProducts', 2)
}

//触发表单
function getFormId(e,that) {
  if (!wx.getStorageSync('formData') || that.data.time != wx.getStorageSync('formData')) {
    wx.setStorageSync('formData', that.data.time)
    wx.setStorageSync('formTime', 0)
  }
  if (wx.getStorageSync('formTime') < 100) {
    var info = {
      'customerUuid': wx.getStorageSync('customerUuid'),
      formId: e.detail.formId
    }
    
    var op = {
      'data': info
    }
    PostData(op, function (res) {
      if (res.data.return_code == '0') {
        wx.setStorageSync('formTime', wx.getStorageSync('formTime') + 1)
      }
    }, 'getFormId', 2)
  }
}

function richFormat(content){
  const regimg = new RegExp('<img', 'gi');
  var tagsName = ['table', 'tr', 'th', 'td','p']
  for (var i in tagsName) {
    var tagName = tagsName[i];
    var regTags = new RegExp('<' + tagName, 'gi');
    content = content.replace(regTags, `<${tagName} class="wx-${tagName}"`)
  }
  content = content.replace(regimg, `<img class="wx-img"`);
  return content;
}

function luYin(callback,callturn){
  wx.authorize({
    scope: 'scope.record',
    success: function () {
      callback(true)
      wx.startRecord({
        success(res) {
          wx.showLoading({
            title: '正在识别',
          })
          video(res.tempFilePath, function (curres) {
            wx.hideLoading()
            if (curres.return_code == '0') {
              var keyword = curres.results[0]
              callturn(keyword)
            } else {
              wx.showToast({
                title: '未能识别',
                icon: 'none'
              })
            }
          }, 'speechFile')
        }
      })

    },
    fail: function (res) {
      callback(false)
      wx.showModal({
        title: '提示',
        content: '未授权录音功能，可以在小程序设置界面（右上角 - 关于 - 右上角 - 设置）中控制对小程序的授权状态。',
      })
    }
  })
}


function transDate(mescStr) {
  var n = mescStr;
  var date = new Date(n);
  var Y = date.getFullYear() + '-';
  var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
  var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
  return (Y + M + D)
}

function getnavigate(action, uuid) {
  if (action === "resetCustomerPwd") {
    wx.switchTab({
      url: '/pages/my/index'
    })
  } else if (action === "cart") {
    wx.reLaunch({
      url: '/pages/cart/index'
    })
  } else if (action === "details") {
    //商品列表-分类
    wx.redirectTo({
      url: '/pages/shop/details?productUuid=' + uuid,
    })
  } else if (action === "index") {
    wx.switchTab({
      url: '/pages/my/index'
    })
  } else {
    wx.navigateBack({
      delta: 1, // 回退前 delta(默认为1) 页面
    })
  }
}

///Score 成绩   保存n位小数
function getscore(Score, n) {
  return ((Score / 5) * 100).toFixed(n);
}

function gettime(time) {
  return /\d{4}-\d{1,2}-\d{1,2}/g.exec(time);
}

// 获取个人用户中心
function Getinfo(that, token, sessionId, customerUuid) {
  var sessionId = wx.getStorageSync('customerUuid');
  var token = wx.getStorageSync('token');
  var customerUuid = wx.getStorageSync('customerUuid');
  that.setData({
    loadingHidden: false
  });
  if (sessionId && token && customerUuid) {
    var info = {
      "customerUuid": customerUuid,
      "token": token,
      "sessionId": sessionId
    }
    var options = {
      'data': info
    }
    PostData(options, function(cusres) {
      that.setData({
        customerCenter: cusres.data.customerIndexInfo
      });
      setTimeout(function() {
        that.setData({
          loadingHidden: true
        })
      }, 1500)
      if (cusres.data.return_code != "0") {
        wx.showToast({
          title: "请先登录",
          icon: 'loading',
          duration: 2000
        })
        wx.navigateTo({
          url: '/pages/my/toLogin',
        })
      }


    }, "customerCenter", 1)
  } else {
    wx.showToast({
      title: "请先登录",
      icon: 'loading',
      duration: 2000
    })
    wx.navigateTo({
      url: '/pages/my/toLogin',
    })
  }
}


//添加/删除---商品/店铺收藏
function addFavorite(uuid, opeType, success) {
  sessionId = wx.getStorageSync('customerUuid');
  token = wx.getStorageSync('token');
  customerUuid = wx.getStorageSync('customerUuid');
  var mes = null;
  if (sessionId && token && customerUuid) {
    var info = [];
    if (opeType == "addStoreFavorite") {
      info = {
        "customerUuid": customerUuid,
        "token": token,
        "sessionId": sessionId,
        "storeUuid": uuid
      }
    } else if (opeType == "deleteStoreFavorite") {
      info = {
        "customerUuid": customerUuid,
        "token": token,
        "sessionId": sessionId,
        "storeFavoriteUuid": uuid
      }
    } else if (opeType == "addProductFavorite") {
      info = {
        "customerUuid": customerUuid,
        "token": token,
        "sessionId": sessionId,
        "productUuid": uuid
      }
    } else if (opeType == "deleteProductFavorite") {
      info = {
        "customerUuid": customerUuid,
        "token": token,
        "sessionId": sessionId,
        "productFavoriteUuid": uuid
      }
    }
    var options = {
      'data': info
    }
    PostData(options, function(cusres) {
      if (cusres.data.return_code == "0") {
        if (opeType == "addStoreFavorite") {
          mes = "店铺收藏成功"
        } else if (opeType == "deleteStoreFavorite") {
          mes = "取消店铺收藏"
        } else if (opeType == "addProductFavorite") {
          mes = "商品收藏成功"
        } else if (opeType == "deleteProductFavorite") {
          mes = "取消商品收藏"
        }
        wx.showToast({
          title: mes,
          duration: 500,
        })
      } else {
        wx.showToast({
          title: cusres.data.message,
          duration: 500,
        })
      }
    }, opeType, 1)
  } else {
    wx.showToast({
      title: "请先登录",
      icon: 'loading',
      duration: 2000
    })
    wx.redirectTo({
      url: '/pages/my/toLogin',
    })
  }
}


// 更新用户资料
function updateinfo(that, thetype, data) {
  var sessionId = '1';
  var token = 'token';
  var customerUuid = wx.getStorageSync('customerUuid');
  if (sessionId && token && customerUuid) {
    var info = [];
    if (thetype == 1) {
      info = {
        "customerUuid": customerUuid,
        "token": token,
        "sessionId": sessionId,
        nickName: data
      }
    } else if (thetype == 2) {
      info = {
        "customerUuid": customerUuid,
        "token": token,
        "sessionId": sessionId,
        gender: data,
      }
    } else if (thetype == 3) {
      info = {
        "customerUuid": customerUuid,
        "token": token,
        "sessionId": sessionId,
        birthday: data
      }
    }

    var options = {
      'data': info
    }
    PostData(options, function(cusres) {
      that.setData({
        customerInfo: cusres.data.customerInfo
      })
    }, "updateCustomerInfo", 2)
  } else {
    wx.showToast({
      title: "请先登录",
      icon: 'loading',
      duration: 2000
    })
    wx.redirectTo({
      url: '/pages/my/toLogin',
    })
  }
}

function ActionTyleComp(actionType) {
  var ACTION_CODE_S_0 = "S_0"; //店铺首页  店铺ID
  var ACTION_CODE_S_1 = "S_1"; //店铺详情  店铺ID
  var ACTION_CODE_P_L_C_0 = "P_L_C_0"; //商品列表-分类  分类ID
  var ACTION_CODE_P_L_O_0 = "P_L_O_0"; //商品列表-品牌  品牌名称
  var ACTION_CODE_P_0 = "P_0"; //商品详情 DetailActivity 商品ID
  var ACTION_CODE_A_0 = "A_0"; //活动内容页面 URL
  var ACTION_CODE_A_0_1 = "A_0_1"; //活动内容页面 内容
  var ACTION_CODE_A_1 = "A_1"; //平台活动商品列表 SpecialFlashSaleActivity 活动id
  var ACTION_CODE_A_2 = "A_2"; //店铺活动商品列表
  var ACTION_CODE_A_3 = "A_3"; //店铺活动列表
  var ACTION_CODE_Q_P_0 = "Q_P_0"; //关键字搜索商品列表 ProductListActivity
  var ACTION_CODE_Q_S_0 = "Q_S_0"; //关键字搜索店铺列表 ProductListActivity
  var ACTION_CODE_M_I_0 = "M_I_0"; //个人中心界面-我的积分 IntegralActivity 判断登录
  var ACTION_CODE_M_C_0 = "M_C_0"; //个人中心界面-我的优惠券 CouponManagerActivity 判断登录
  var ACTION_CODE_M_S_0 = "M_S_0"; //个人中心界面-店铺收藏  CollectionShopActivity KEY_INENT_COLLECTION_INDEX
  var ACTION_CODE_M_P_0 = "M_P_0"; //个人中心界面-商品收藏  CollectionShopActivity KEY_INENT_COLLECTION_INDEX
  var ACTION_CODE_P_SC_0 = "P_SC_0"; //商品橱窗
  var ACTION_CODE_S_RM_0 = "S_RM_0"; //店铺推荐  RecommendStoreActivity  不需要参数

  var json = JSON.parse(actionType);
  var actionCode = json.actionCode;
  var actionId = json.actionId;
  var actionTitle = json.actionTitle;


  if (ACTION_CODE_S_0 == actionCode) {
    //店铺首页
    wx.navigateTo({
      url: '/pages/store/index?storeUuid=' + actionId,
    })
  } else if (ACTION_CODE_S_1 == actionCode) {
    //店铺详情
    wx.navigateTo({
      url: '/pages/store/stroedetails?storeUuid=' + actionId,
    })
  } else if (ACTION_CODE_P_L_C_0 == actionCode) {
    //商品列表-分类
    wx.navigateTo({
      url: '/pages/shop/list?categoryUuid=' + actionId,
    })
  } else if (ACTION_CODE_P_L_O_0 == actionCode) {
    //商品列表-品牌
    wx.navigateTo({
      url: '/pages/shop/list?searchKey=' + actionId,
    })
  } else if (ACTION_CODE_P_0 == actionCode) {
    //商品详情 DetailActivity 商品ID
    wx.navigateTo({
      url: '/pages/shop/details?productUuid=' + actionId,
    })
  } else if (ACTION_CODE_A_0 == actionCode) {
    //活动内容页面 URL
    wx.showToast({
      title: "小程序不支持外部链接！",
      icon: 'loading',
      duration: 2000
    })
    // wx.navigateTo({
    //     url: 'notice?desc=' + actionId+'&title='+actionTitle,
    // })
  } else if (ACTION_CODE_A_0_1 == actionCode) {
    //活动内容页面 内容
    wx.setStorageSync('notice', actionId);
    wx.navigateTo({
      url: "notice?title=" + actionTitle,
    })
  } else if (ACTION_CODE_A_1 == actionCode) {
    //平台活动商品列表 SpecialFlashSaleActivity 活动id
    wx.navigateTo({
      url: '/pages/store/index?storeUuid=' + actionId,
    })
  } else if (ACTION_CODE_A_2 == actionCode) {
    //店铺活动商品列表
    wx.navigateTo({
      url: '/pages/store/index?storeUuid=' + actionId,
    })
  } else if (ACTION_CODE_A_3 == actionCode) {
    //店铺活动列表
    wx.navigateTo({
      url: '/pages/store/stroedetails?storeUuid=' + actionId,
    })
  } else if (ACTION_CODE_Q_P_0 == actionCode) {
    //关键字搜索商品列表 ProductListActivity
    wx.navigateTo({
      url: '/pages/shop/list?categoryUuid=' + actionId,
    })
  } else if (ACTION_CODE_Q_S_0 == actionCode) {
    //关键字搜索店铺列表 ProductListActivity
    wx.navigateTo({
      url: '/pages/shop/list?searchKey=' + actionId,
    })
  } else if (ACTION_CODE_M_I_0 == actionCode) {
    //个人中心界面-我的积分 IntegralActivity 判断登录
    if (sessionId && token && customerUuid) {
      wx.navigateTo({
        url: '/pages/my/integrals/myintegrals',
      })
    } else {
      wx.showToast({
        title: "请先登录",
        icon: 'loading',
        duration: 2000
      })
      wx.redirectTo({
        url: '/pages/my/toLogin',
      })
    }
  } else if (ACTION_CODE_M_C_0 == actionCode) {
    //个人中心界面-我的优惠券 
    if (sessionId && token && customerUuid) {
      wx.navigateTo({
        url: '/pages/my/coupon/list',
      })
    } else {
      wx.showToast({
        title: "请先登录",
        icon: 'loading',
        duration: 2000
      })
      wx.redirectTo({
        url: '/pages/my/toLogin',
      })
    }
  } else if (ACTION_CODE_M_S_0 == actionCode) {
    //个人中心界面-店铺收藏
    if (sessionId && token && customerUuid) {
      wx.navigateTo({
        url: '/pages/my/Favorite/list?tab=2',
      })
    } else {
      wx.showToast({
        title: "请先登录",
        icon: 'loading',
        duration: 2000
      })
      wx.navigateTo({
        url: '/pages/my/toLogin',
      })
    }
  } else if (ACTION_CODE_M_P_0 == actionCode) {
    //个人中心界面-商品收藏
    if (sessionId && token && customerUuid) {
      wx.navigateTo({
        url: '/pages/my/Favorite/list?tab=1',
      })
    } else {
      wx.showToast({
        title: "请先登录",
        icon: 'loading',
        duration: 2000
      })
      wx.redirectTo({
        url: '/pages/my/toLogin',
      })
    }
  } else if (ACTION_CODE_P_SC_0 == actionCode) {
    //商品橱窗

  } else if (ACTION_CODE_S_RM_0 == actionCode) {
    //店铺推荐  
    wx.navigateTo({
      url: '/pages/store/stroelist',
    })
  }
}


//上传文件
function upload_file(filePath, name, formData, success, fail) {
  wx.uploadFile({
    url: plublic.IMGURLLINK,
    filePath: filePath,
    name: name,
    header: {
      'content-type': 'multipart/form-data'
    }, // 设置请求的 header
    formData: formData, // HTTP 请求中其他额外的 form data
    success: function(res) {
      //console.log(res);
      if (res.statusCode == 200 && !res.data.result_code) {
        // console.log("imgName:" + res.data.imgName);
        typeof success == "function" && success(res.data);
      } else {
        typeof fail == "function" && fail(res);
      }
    },
    fail: function(res) {
      // console.log(res);
      typeof fail == "function" && fail(res);
    }
  })
}

//头像上传文件
function txupload_file(filePath, name, success, fail) {
  var sessionId = wx.getStorageSync('customerUuid');
  var token = wx.getStorageSync('token');
  var customerUuid = wx.getStorageSync('customerUuid');
  var vmap = {
    "customerUuid": customerUuid,
    "token": token,
    "sessionId": sessionId
  }
  var jsonParam = {
    "opeType": 'updateHead',
    "map": vmap
  }
  wx.uploadFile({
    url: plublic.TXIMGURLLINK,
    filePath: filePath,
    name: name,
    header: {
      'content-type': 'multipart/form-data'
    }, // 设置请求的 header
    formData: {
      "jsonParam": JSON.stringify(jsonParam),

    }, // HTTP 请求中其他额外的 form data
    success: function(res) {
      if (res.statusCode == 200 && !res.data.result_code) {
        typeof success == "function" && success(res.data);
      } else {
        typeof fail == "function" && fail(res);
      }
    },
    fail: function(res) {
      // console.log(res);
      typeof fail == "function" && fail(res);
    }
  })
}

//售后上传文件
function shupload_file(filePaths, callback, name, i, length, imgNames) {
  var imgName = []
  if (imgNames) {
    imgName = imgNames
  }
  wx.uploadFile({
    url: plublic.IMGURLLINK,
    filePath: filePaths[i],
    name: name,
    header: {
      'content-type': 'multipart/form-data',
      'content-type': 'application/json'
    }, // 设置请求的 header
    formData: {
      'jsonParam': ''
    },

    success: function(res) {
      if (res.statusCode == 200) {
        var str1 = JSON.parse(res.data);
        var pics = str1.pics;
        imgName.push(pics[0].imgName);
      } else {
        callback(false)
      }
    },
    fail: function(res) {
      wx.hideLoading()
      wx.showToast({
        title: '上传失败',
        icon:'none'
      })
      callback(false)
    },
    complete: function(res) {
      i++;
      if (i == length) {
        callback(imgName)
      } else { //递归调用uploadDIY函数
        shupload_file(filePaths, callback, name, i, length, imgName);
      }
    },
  })
}


//录音上传
function video(filePaths, callback, name) {
  console.log(filePaths)
  wx.uploadFile({
    url: plublic.VIDEO,
    filePath: filePaths,
    name: name,
    header: {
      'content-type': 'multipart/form-data',
      'content-type': 'application/json'
    }, // 设置请求的 header
    formData: {
      'jsonParam': ''
    },

    success: function (res) {
      if (res.statusCode == 200) {
        var str1 = JSON.parse(res.data);
        callback(str1)
      } else {
        callback(false)
      }
    },
    fail: function (res) {
      callback(false)
    },
    complete: function (res) {
    },
  })
}



//评价图片上传
function pjupload_file(filePaths, name, i, successUp, failUp, length, puuid) {
  wx.uploadFile({
    url: plublic.IMGURLLINK,
    filePath: filePaths[i],
    name: name,
    header: {
      'content-type': 'multipart/form-data',
      'content-type': 'application/json; charset=utf-8'
    }, // 设置请求的 header
    success: function(res) {
      successUp++;
      if (res.statusCode == 200) {
        typeof success == "function" && success(res.data);
        var str1 = JSON.parse(res.data);
        var pics = str1.pics;
        var picsarr = wx.getStorageSync('pjpics') || []
        //  var exist = picsarr.filter(function (ele) {
        //     return ele.puuid === puuid
        // })[0];
        // if (!exist) {
        picsarr.push({
          puuid: puuid,
          picName: pics[0].imgName,
          picUrl: pics[0].imgPath
        });
        // }
        wx.setStorageSync('pjpics', picsarr);
      } else {
        typeof fail == "function" && fail(res);
      }
    },
    fail: function(res) {
      failUp++;
      console.log(res);
      typeof fail == "function" && fail(res);
    },
    complete: function(res) {
      i++;
      if (i == length) {
        console.log('总共' + successUp + '张上传成功,' + failUp + '张上传失败！');
        app.globalData.isupload = 1;
      } else { //递归调用uploadDIY函数
        pjupload_file(filePaths, name, i, successUp, failUp, length, puuid);
      }
    },
  })
}


//6.3微信上传图片接口
function wxupload_file(media_id, success, fail) {
  var sessionId = wx.getStorageSync('customerUuid');
  var token = wx.getStorageSync('token');
  var customerUuid = wx.getStorageSync('customerUuid');
  var info = {
    "customerUuid": customerUuid,
    "token": token,
    "sessionId": sessionId,
    "media_id": media_id
  }
  var options = {
    'data': info
  }

  PostData(options, function(res) {
    if (res.statusCode == 200 && res.data.return_code == "0") {
      var info = {
        "customerUuid": customerUuid,
        "token": token,
        "sessionId": sessionId,
        "image": res.data.imgModel.imgName
      }
      var options = {
        'data': info
      }
      PostData(options, function(res) {}, "uploadHeadImage", 1)
    } else {
      typeof fail == "function" && fail(res);
    }

  }, "weixinImgUpload", 1)
}

// 更新地址管理资料
function modifyCustomerAddress(that, thetype, uuid,callback) {
  var sessionId = wx.getStorageSync('sessionId');
  var token = wx.getStorageSync('token');
  var customerUuid = wx.getStorageSync('customerUuid');
  var info = [];
  if (thetype == 1) {
    info = {
      "customerUuid": customerUuid,
      "token": token,
      "sessionId": sessionId,
      "addressUuid": uuid,
    }
    var options = {
      'data': info
    }
    PostData(options, function(cusres) {
      if (cusres.data.return_code=='0'){
        callback(true)
      }
    }, "setDefaultCustromerAddress", 2)
  } else if (thetype == 2) {
    info = {
      "customerUuid": customerUuid,
      "token": token,
      "sessionId": sessionId,
      "uuid": uuid,
    }
    var options = {
      'data': info
    }
    PostData(options, function(cusres) {}, "modifyCustomerAddress", 1)
  } else if (thetype == 3) {
    wx.showModal({
      title: '温馨提示',
      content: ' 确认要删除地址吗？',
      confirmColor: '#F25630',
      success: function(res) {
        if (res.confirm) {
          var info = {
            "customerUuid": customerUuid,
            "token": token,
            "sessionId": sessionId,
            "addressUuid": uuid,
          }
          var options = {
            'data': info
          }
          wx.showLoading({
            title: '正在删除',
          })
          PostData(options, function(cusres) {
            if (cusres.data.return_code != '0') {
              wx.showToast({
                title: cusres.data.message,
                icon: 'none'
              })
            }else{
              callback(true)
            }
          }, "deleteCustomerAddress", 1);
        }
      }
    })
  }

}

//删除订单
function deleteorder(that, orderUuid) {
  var sessionId = wx.getStorageSync('customerUuid');
  var token = wx.getStorageSync('token');
  var customerUuid = wx.getStorageSync('customerUuid');
  wx.showModal({
    title: '删除',
    content: ' 是否确定删除',
    success: function(res) {
      if (res.confirm) {
        var info = {
          "customerUuid": customerUuid,
          "token": token,
          "sessionId": sessionId,
          "orderUuid": orderUuid
        }
        var options = {
          'data': info
        }
        PostData(options, function(cusres) {
          wx.showToast({
            title: "删除" + cusres.data.message,
            icon: 'loading',
            duration: 2000,
            success: function(ssres) {}
          })
        }, "deleteOrder", 1);
      }
    }
  })
}

//确认收货
function confirmReceive(that, orderUuid) {
  var sessionId = wx.getStorageSync('customerUuid');
  var token = wx.getStorageSync('token');
  var customerUuid = wx.getStorageSync('customerUuid');
  wx.showModal({
    title: '确定收货',
    content: ' 是否确定收货',
    success: function(res) {
      if (res.confirm) {
        var info = {
          "customerUuid": customerUuid,
          "token": token,
          "sessionId": sessionId,
          "orderId": orderUuid
        }
        var options = {
          'data': info
        }
        PostData(options, function(cusres) {
          wx.showToast({
            title: "确认收货" + cusres.data.message,
            icon: 'loading',
            duration: 2000,
            success: function(ssres) {}
          })
        }, "orderConfirmReceive", 1);
      }
    }
  })
}

function CheckMobile(mobile) {
  if (!mobile.match(/^1[3-9][0-9]\d{8}$/)) {
    wx.showModal({
      title: '错误',
      content: '请输入正确的手机号'
    })
    return false
  }
  return true
}

function Checkpsw(psw) {
  if (!psw.match(/^.{6,20}$/)) {
    wx.showModal({
      title: '错误',
      content: '请输入正确的6-20个字符，可以是数字、字母和符号'
    })
    return false
  }
  return true
}

function Getreplace(str) {
  return str.replace('-', ',')
}

function Getorderopvalues(optionsvalue, num, value) {
  var optionsvalues = optionsvalue.split("-");
  var str = '';
  for (let i = 0; i < optionsvalues.length; i++) {
    if (i == num) {
      optionsvalues[i] = value;
    }
    str += optionsvalues[i] + '-';
  }
  str = str.substr(0, str.length - 1);
  wx.setStorageSync('optionsvalues', str);
  return str;
}

function getproductAndAttrIds(arr) {
  var ret = '';
  for (var i = 0; i < arr.length; i++) {
    ret += arr[i] + ',';
  }
  ret = ret.substr(0, ret.length - 1);
  return ret;
}

function getproductUuidAndSkuNoIdArr(arr) {
  var ret = '';
  for (var i = 0; i < arr.length; i++) {
    ret += arr[i] + '|';
  }
  ret = ret.substr(0, ret.length - 1);
  return ret;
}

function removeByValue(arr, val) {
  //去除指定数组
  for (var i = 0; i < arr.length; i++) {
    if (arr[i] == val) {
      arr.splice(i, 1);
      break;
    }
  }
  return arr;
}

function djsfreshtime(leftsecond) {
  var re = '';
  if (leftsecond > 0) {
    //还有一个小bug，当事件差为负数时，值为负数，将上面leftsecond代码改一下
    var leftsecond = parseInt(Math.abs(leftsecond) / 1000);
    var d = parseInt(leftsecond / (3600 * 24)); //计算出相差天数
    d = d > 0 ? d + '天' : '';
    var h = parseInt((leftsecond / 3600) % 24); //扣除相差天数，计算出相差小时数
    h = h > 0 ? h + ':' : '';
    var m = parseInt((leftsecond / 60) % 60); //扣除相差天数，小时数，计算出相差分钟数
    m = m > 0 ? m + ':' : '';
    var s = parseInt(leftsecond % 60); //扣除相差天数、小时数、分钟数相差数，计算出相差秒速
    re = d + h + m + s;
  } else {
    re = '0';
  }
  return re
}

function freshtime(see, end, sys) {
  var endtime = new Date(end);
  var nowtime = new Date(sys);
  var re = '';
  if (endtime.getTime() - nowtime.getTime() > 0 && see) {
    //还有一个小bug，当事件差为负数时，值为负数，将上面leftsecond代码改一下
    var leftsecond = see;
    var d = parseInt(leftsecond / (3600 * 24)); //计算出相差天数
    d = d > 0 ? d + '天' : '';
    var h = parseInt((leftsecond / 3600) % 24); //扣除相差天数，计算出相差小时数
    h = h > 0 ? h + ':' : '';
    var m = parseInt((leftsecond / 60) % 60); //扣除相差天数，小时数，计算出相差分钟数
    m = m > 0 ? m + ':' : '';
    var s = parseInt(leftsecond % 60); //扣除相差天数、小时数、分钟数相差数，计算出相差秒速
    var re = d + h + m + s;
  } else {
    re = '已结束';
  }
  return re
}